<template>
  <div>
    <!-- 定义面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>博客管理</el-breadcrumb-item>
      <el-breadcrumb-item>博客列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 定义卡片 -->
    <el-card class="box-card">
      <!-- 定义第一行数据 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入博客标题"
            v-model="queryItemInfo.query"
            clearable
            @clear="getBlogList"
          >
            <el-button slot="append" icon="el-icon-search" @click="getBlogList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="toAddBlog">添加博客</el-button>
        </el-col>
      </el-row>

      <!-- 获取博客列表数据 -->
      <el-table :data="blogList" style="width: 100%" stripe border>
        <el-table-column type="index" label="序号" width="50px"></el-table-column>
        <!--  <el-table-column prop="picName" label="标题图"></el-table-column> -->
        <el-table-column prop="title" label="博客标题" width="250px"></el-table-column>
        <el-table-column prop="author" label="作者">
          <template slot-scope="scope">
            <div slot="reference" class="name-wrapper">
              <el-tag size="medium">{{ scope.row.author==null?'无':scope.row.author }}</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="blogSort.sortName" label="博客分类">
          <template slot-scope="scope">
            <div slot="reference" class="name-wrapper">
              <el-tag size="medium" type="success">{{ scope.row.blogSort.sortName }}</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="blogTag" label="博客标签" width="200px">
          <template slot-scope="scope">
            <div slot="reference" class="name-wrapper">
              <el-tag
                size="medium"
                v-for="item in scope.row.tagList"
                :key="item.uid"
                type="warning"
              >{{ item.content }}</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="isPublish" label="是否发布">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.isPublish"
              :active-value="'1'"
              :inactive-value="'0'"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="updateStatus(scope.row)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="180px"></el-table-column>
        <el-table-column label="操作" width="180px">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="updateBlogBtn(scope.row)"
            >修改</el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="deleteBlogBtn(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 定义分页插件-->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryItemInfo.pageNum"
        :page-sizes="[1, 5, 10, 20]"
        :page-size="queryItemInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //1.定义博客列表信息
      blogList: [],
      //2.定义分页对象
      queryItemInfo: {
        query: '', //定义查询参数
        pageNum: 1,
        pageSize: 10
      },
      total: 0
    }
  },
  created() {
    //1.获取博客列表数据
    this.getBlogList()
  },
  methods: {
    //实现博客信息分页查询
    async getBlogList() {
      const {
        data: result
      } = await this.$http.get("/blog/getList", {
        params: this.queryItemInfo
      })
      if (result.status !== 200) return this.$message.error("博客列表查询失败")
      this.blogList = result.data.rows
      this.total = result.data.total
    },
    //通过JS格式化时间
    formatDate(row, column, cellValue, index) {
      let date = new Date(cellValue)
      let year = date.getFullYear()
      let month = (date.getMonth() + 1 + '').padStart(2, '0')
      let day = (date.getDate() + '').padStart(2, '0')
      let HH = (date.getHours() + '').padStart(2, '0')
      let MM = (date.getMinutes() + '').padStart(2, '0')
      let SS = (date.getSeconds() + '').padStart(2, '0')
      return year + '-' + month + '-' + day + ' ' + HH + ":" + MM + ":" + SS
    },
    //条数变化时 调用
    handleSizeChange(size) {
      this.queryItemInfo.pageSize = size
      this.getBlogList()
    },
    //页码值变化时 调用
    handleCurrentChange(current) {
      this.queryItemInfo.pageNum = current
      this.getBlogList()
    },
    async updateStatus(blog) {
      const {
        data: result
      } = await this.$http.post("/blog/isPublish", blog)
      if (result.status !== 200) return this.$message.error("发布失败")
      if (blog.isPublish == 1) {
        this.$message.success("发布成功")
      }else{
        this.$message.success("取消成功")
      }
    },
    async deleteBlogBtn(blog) {
      //消息确认框
      this.$confirm(`此操作将删除该博客, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        //根据id删除数据
        const {
          data: result
        } = await this.$http.post("/blog/updateStatus", blog)
        if (result.status !== 200) return this.$message.error("博客删除失败")
        this.$message.success("博客删除成功")
        //重新获取博客列表信息
        this.getBlogList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    //转向到博客新增页面
    toAddBlog() {
      this.$router.push("/blog/addBlog")
    },
    //转向博客修改页面
    updateBlogBtn(blog) {
      //将博客数据存一份导入到编辑页面去
      window.sessionStorage.setItem("blog", JSON.stringify(blog))
      this.$router.push("/blog/updateBlog")
    }
  }
}
</script>
<!-- 防止组件样式冲突 -->
<style lang="less" scoped>
</style>
